package com.bugsnag.ohos;

import com.google.gson.stream.JsonReader;

import java.io.IOException;

/**
 * User
 *
 * @since 2021-07-01
 */
public class User implements JsonStream.Streamable, JsonReadable<User> {
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_EMAIL = "email";

    /**
     * the user ID, by default a UUID generated on installation
     */
    private String id;

    /**
     * the user's email, if available
     */
    private String email;

    /**
     * the user's name, if available
     */
    private String name;

    /**
     * constructor
     */
    public User() {
        this(null, null, null);
    }

    /**
     * constructor
     *
     * @param id
     * @param email
     * @param name
     */
    public User(String id, String email, String name) {
        this.id = id;
        this.email = email;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public User fromReader(JsonReader reader) {
        User user = null;
        try {
            reader.beginObject();
            String userId = null;
            String userEmail = null;
            String userName = null;
            while (reader.hasNext()) {
                String key = reader.nextName();
                String value = reader.nextString();
                if (key.equals(KEY_ID)) {
                    userId = value;
                } else if (key.equals(KEY_NAME)) {
                    userEmail = value;
                } else if (key.equals(KEY_EMAIL)) {
                    userName = value;
                }
            }
            user = new User(userId, userEmail, userName);
            reader.endObject();
        } catch (IOException e) {
            e.getMessage();
        }
        return user;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (other instanceof User) {
            if (User.this.id.equals(((User) other).id)) {
                return false;
            }
            if (User.this.email.equals(((User) other).email)) {
                return false;
            }
            if (User.this.name.equals(((User) other).name)) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name(KEY_ID).value(id);
        writer.name(KEY_EMAIL).value(email);
        writer.name(KEY_NAME).value(name);
        writer.endObject();
    }
}
